using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;


namespace UFrame
{
    /// <summary>
    /// 引导的一步骤
    /// </summary>
    public class RunGuild
    {
        public string panelName;
        public string clipName;
        public int majorStep;
        public int index;
        public Transform target;
        public GuideItem def;
    }

    /// <summary>
    /// 新手引导
    /// </summary>
    public partial class UIGuide : BaseUI
    {
        public GameObject fxFingerClick; // 指头点击
        public GameObject fxCircle; // 圆圈
        public GameObject fxJoyStick; // 摇杆
        public GameObject fxSigned; // 仅指示方框
        public GameObject fxCircleWithoutEvent; // 仅指示圆框
        public GameObject backgroundMask;
        public Image imageMask;
        public GameObject curFinger;

        private bool waitEvent = false;         //当前步骤:


        public virtual void OnInit()
        {
            backgroundMask.gameObject.SetActive(false);
            eventc.RegisterEvent(StartLoadSceneArgs.EventId, () => { waitEvent = true; });
        }

        protected void OnDisable()
        {
            
            ClearGuide();
        }

        public void ClearGuide()
        {
            Destroy(curFinger);
            backgroundMask.gameObject.SetActive(false);
        }

        private bool WaitUntilEvent()
        {
            return waitEvent;
        }

        private void StartGuideCallback(int majorStep, int smallIndex)
        {
            backgroundMask.gameObject.SetActive(true);

            if (smallIndex >= GuideMa.I.curGroup.Count)
            {
                backgroundMask.gameObject.SetActive(false);
                GuideMa.I.RequestRecordGuideStep(majorStep);
                return;
            }

            var curGuid = GuideMa.I.GetCur(smallIndex);
        }


        public void FindTransformByAddress(string address, Action<string, Transform> action)
        {
            Action<string, Transform> uiAction = action;

            string[] address_segs = address.Split('/');
            if (address_segs.Length < 2)
            {
                return;
            }

            string uiName = address_segs[1];
            var segs = address_segs[1].Split(',');
            uiName = segs[0];

            string args = null;
            if (segs.Length == 2)
            {
                args = segs[1];
                //uiAdress = address.Replace(address_segs[1], segs[0]);
            }

            // 第一次打开的面板，等待面板打开
            UIMa.ShowUI(uiName, args);
            StopCoroutine(FindUIIE());
            waitEvent = false;
            StartCoroutine(FindUIIE());
        }

        private IEnumerator FindUIIE()
        {
            var curGuid = GuideMa.I.curGuide;
            if (GuideMa.I.curGuide.eventId != 0)
            {
                yield return new WaitUntil(WaitUntilEvent);
                waitEvent = false;
            }

            if (curGuid != null && curGuid.findControlInterval > 0)
            {
                yield return new WaitForSeconds(curGuid.findControlInterval);
            }

            /*target = uiRoot.transform.Find(uiAdress);
            if (target == null) // 再找一次
            {
                Helper.WaitForCondition(WaitForConditionType.等待新手引导,
                    () => { FindTransformByAddress(uiAdress, uiAction); },
                    () =>
                    {
                        Debug.LogWarning(string.Format("检查新手引导配置 path = {0}", uiAdress));
                        WizardInfoManager.instance.isPlaying = false;
                        blackMask.SetActive(false);
                        backgroundMask.gameObject.SetActive(false);
                        GEM.instance.OnEvent<string>(GameEventEnum.MoveTips, "引导目标为空或顺序有误");
                    },
                    () =>
                    {
                        var t = uiRoot.transform.Find(uiAdress);
                        return t != null;
                    }, 3f);
            }
            else
            {
                uiAction(panelName, target);
            }*/

            yield return null;
        }



        //关闭UI启动新手引导
        public void ShowCacheUIWaitGuide(string ui)
        {

        }


        //显示缓冲新手引导
        public void ShowCacheGuide()
        {

        }




    }
}